﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sys.WebRes.Data;
using Sys.WebRes.Publish.Areas.Admin.Models;

namespace Sys.WebRes.Publish.Areas.Admin.Controllers
{
    public class DropdownController : Controller
    {
        public static int Timeout = 0;

        [OutputCache(Duration = 10000, VaryByParam="Timeout")]
        public ActionResult Users()
        {
            using (var db = new ResourceSysEntities())
            {
                var json = db.UserTBL.OrderBy(p => p.RegisterDate).Select(p => new { value = p.ID, text = p.Name }).ToList();
                json.Insert(0, new { value = 0, text = "--请选择--" });
                return this.Json(json, JsonRequestBehavior.AllowGet);
            }
        }

        [OutputCache(Duration = 10000, VaryByParam = "Timeout")]
        public ActionResult Users1()
        {
            using (var db = new ResourceSysEntities())
            {
                var json = db.UserTBL.OrderBy(p => p.RegisterDate).Select(p => "<option value=''>" + p.Name + "</option>").ToList();
                json.Insert(0, "<option value='0'>--请选择--</option>");

                return Content(string.Join("\r\n", json.ToArray()));
            }
        }


        [OutputCache(Duration = 10000, VaryByParam = "Timeout")]
        public ActionResult ResType()
        {
            using (var db = new ResourceSysEntities())
            {
                var json = db.ResTypeTBL.OrderBy(p => p.OrderNo).Select(p => new { value = p.ID, text = p.Name }).ToList();

                return this.Json(json, JsonRequestBehavior.AllowGet);
            }
        }

        [OutputCache(Duration = 10000, VaryByParam = "Timeout")]
        public ActionResult StatusType()
        {
            using (var db = new ResourceSysEntities())
            {
                var json = db.ResStatusTypeTBL.OrderBy(p => p.OrderNo).Select(p => new { value = p.ID, text = p.Name }).ToList();
                return this.Json(json, JsonRequestBehavior.AllowGet);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [OutputCache(Duration = 10000, VaryByParam = "Timeout")]
        public ActionResult Section()
        {
            using (var db = new ResourceSysEntities())
            {
                var map = db.SectionTBL.Where(p => p.DeletedFlag == false)
                                        .OrderBy(p => p.OrderNo)
                                        .Select(p => new SectionModel{
                                            ID = p.ID,
                                            Name = p.Name,
                                            OrderNo = p.OrderNo,
                                            ParentID = (int?)p.Parent.ID,
                                            LevelNo = p.LevelNo})
                                        .ToDictionary(k => k.ID, v => v);


                var list = new List<SectionModel>();
                list.Add(new SectionModel() { ID = 0, Name = "--请选择--" });

                foreach (var item in map)
                {
                    if (item.Value.ParentID != null)
                    {
                        int key = item.Value.ParentID ?? 0;
                        map[key].children.Add(item.Value);
                    }
                    else
                    {
                        list.Add(item.Value);
                    }
                }

                return Json(list, JsonRequestBehavior.AllowGet); ;
            }
        }

        [OutputCache(Duration = 10000, VaryByParam = "Timeout")]
        public ActionResult Tags()
        {
            using (var db = new ResourceSysEntities())
            {
                var json = db.TagTBL.GroupJoin(db.TagResLinkTBL, l=>l.ID, r => r.TagID, 
                    (l,r)=>new {id = l.ID, text = l.Name, selected = true}).ToList();

                //var json = db.TagTBL.OrderBy(p => p.Name).Select(p => new { value = p.ID, text = p.Name }).ToList();
                return this.Json(json, JsonRequestBehavior.AllowGet);
            }
        }
        [OutputCache(Duration = 10000, VaryByParam = "Timeout")]
        public ActionResult TagView()
        {
            using (var db = new ResourceSysEntities())
            {
                var list = db.TagTBL.GroupJoin(db.TagResLinkTBL, l => l.ID, r => r.TagID,
                            (l, r) => new TagModel{ ID = l.ID, Name = l.Name, Count = r.Count()  }).ToList();

                return this.View(list);
            }
        }
    }
}